package com.zlx.store.coupon.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlx.store.coupon.constants.CouponConstant;
import com.zlx.store.coupon.dao.CouponDao;
import com.zlx.store.domain.entity.Coupon;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 用于定时更新布隆过滤器
 */
@Component
public class BfCouponJob extends QuartzJobBean {
    @Autowired
    CouponDao couponDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper();
        wrapper.select(Coupon::getCouponId);
        couponDao.selectList(wrapper).forEach(
                c->{
                    Long result = stringRedisTemplate.opsForValue().getOperations()
                            .execute(new DefaultRedisScript<Long>(CouponConstant.luaScript, Long.TYPE)
                                    , Arrays.asList(CouponConstant.couponBF),
                                    c.getCouponId().toString());
                }
        );

    }
}
